﻿#ifndef PST_THREEDSURFACEPLOTWINDOW_H
#define PST_THREEDSURFACEPLOTWINDOW_H

#include "Graph3DWindowBase.h"
#include <vtkSmartPointer.h>
#include <QHash>
#include "ThreeDSurfacePlotSettingData.h"

class vtkProp;
class QStandardItem;
class QListWidgetItem;
class vtkScalarBarActor;
class vtkStructuredGrid;
class vtkTable;

namespace pst
{
    class PropPickerCallbackHander;
    class ThreeDSurfacePlotSettingDialog;
    class ThreeDSurfacePlotSidebarSettings;
    class ThreeDSurfacePlotSidebarData;
    class TDSPDefaultInteractorStyle;

    class ThreeDSurfacePlotWindow :public Graph3DWindowBase
    {
        Q_OBJECT
    public:
        ThreeDSurfacePlotWindow(QWidget* parent, int windowId);

        void updateClipper(bool activeClipper) override;

        void updateClipperPlaneActor();

        void setWindowID(int windowID) override;
        int getWindowID() const override;

        void setWindowUniversalID(int windowUniversalID) override;
        int getWindowUniversalID() const override;

        /// @brief 文件写出至工程文件
        QDomElement& writeToProjectFile(QDomDocument* doc, QDomElement* element, bool isdiso = false) override;

        /// @brief 从工程文件读入数据
        void readFromProjectFile(QDomElement* element, bool isdiso = false) override;

        void addGraph3D(InfomationGraph3DBasical* info) override;

        void setActiveAnimation(bool active) override;

        void exportAnimationData(const QString& path) override;

        void exportSourceDataToLocal() override;

        void pyExportSourceDataToLocal(QString parms) override;

        // 根据数据判断是否可以启用动图
        bool whetherCanEnableDataAnimation() override;

        // 更新db 和 unit
        void updateDBAndUnitData() override;

        vtkSmartPointer<TDSPDefaultInteractorStyle> getDefaultInteractorStyle()
        {
            return m_defaultInteractorStyle;
        }

        virtual void setInteractorStlyleToTranslation() override;
        virtual void setInteractorStlyleToRotation()override;
        virtual void setInteractorStlyleToDefault()override;

    public://python调用
        void pyCurveNameClicked(QList<int> selectedIndexs);
        void pyFrequencyChanged(int index);
        void pyResultDataChanged(int index);
        void pyDataOprationChanged(bool isLoged, bool isNormalized);
        void pyItemCheckStateChanged(int itemIndex, bool visible);
        void pySaveThreeDSurfacePlotMainSettingData(int settingDataId, bool scalarBarIsAutoRange,
            double scalarBarMin, double scalarBarMax, bool scalarBarIsShowTitle,
            QString scalarBarTitle, QString scalarBarFontColor, int scalarBarFontSize,
                                                    int geometryTransparency, QString resultGraphName, int resultGraphModelTransparency, double resultGraphScale, bool cubeAxesIsVisible);
        void pySaveThreeDSurfacePlotMoveResultDataSetting(int infomationId, double translationX, double translationY, double translationZ,
                                                          double rotationTheta, double rotationPhi);
        void pyDelete3DSurfacePlots(QString selectedIndexs);
        void pyThreeDSurfaceCurveNameItemRename(int itemIndex, QString newName);
        void pyThreeDSurfaceCurveDataFileterRadioButtonClicked(int windowId, int index);

    private:
        void initScalarBarSettingConnection();
        void initSidebarSettingConnection();
        void generateSidebarSettingData(QListWidgetItem* item, InfomationGraph3DBasical* info);

        void connectSignalsAndSlots();
        void disconnectSignalsAndSlots();

        void prepareAnimationData();//准备动画数据
        void updateControlPanel() override;
        void animationToNextDataFrame() override;
        bool processAndWriteTables(QList<InfomationGraph3DBasical*> infoList, const QString& outputFilename);
        void removeActorWithMarker(InfomationGraph3DBasical* info);

        void updateMarkerValue() override;//更新marker的值，比如重新选择了场值，动图更新数据
        //void setInteractorStlyleToDefault() override;//设置默认的交互样式

        void updateActorPosition(InfomationGraph3DBasical* info);//更新actor的位置

        //单个info是否包含多个数据，是否可以用于动图
        bool isInfoContainsMultipleData(const InfomationGraph3DBasical* info) override;
        //判断多个info是否是同一来源，是否可以用于动图
        bool isInfoFromSameSource(QList< InfomationGraph3DBasical*> infoList) override;

        void sentToolBarAnimationDataInfo(const InfomationGraph3DBasical* info) override;

        QString generateGraphNameAuto(InfomationGraph3DBasical* info) const;

        ThreeDSurfacePlotSettingData* getMainSettingData(InfomationGraph3DBasical* info);

        ThreeDSurfacePlotSidebarData* getSidebarDataFromNameItem(QListWidgetItem* item);
        InfomationGraph3DBasical* getGraph3DInfoFromSidebarData(ThreeDSurfacePlotSidebarData* data);

        void updateGeometryActors(bool isShow, bool& hasAnyGeometrySuccess);

    public slots:
        void slot_scalarBarDoubleClicked(vtkProp* pickedProp) override;
        void slot_scalarBarFontTypeChanged(vtkScalarBarActor* actor, QString fontType);
        void slot_updateSetting(ThreeDSurfacePlotSettingData* data);
        void slot_showHideItemActor(QListWidgetItem* item);
        void slot_graphNameDoubleClicked(QListWidgetItem* item);
        void slot_updateActorValue(ThreeDSurfacePlotSidebarData* data);
        void slot_chosenItemsChanged(const QList<QListWidgetItem*>& itemList);
        void slot_updateActorOriginalData(ThreeDSurfacePlotSidebarData* data);
        void slot_updateMathOperation(ThreeDSurfacePlotSidebarData* data);
        void slot_deleteChosenGraphs(const QList<QListWidgetItem*>& itemList);

        //重写qt窗口右键菜单 暂时禁用
        void slot_addCustomContextMenu(const QPoint& pos) override;
        //连接vtk交互器的右键菜单
        void slot_rightButtonUpEvent(const QPoint& globalPos, vtkActor* pickedActor);

        void slot_getSidebarCurveDataSource(QListWidgetItem* item, QString& dataSource);
        void slot_renameCheck();
        bool slot_checkResultNameIsValid(ThreeDSurfacePlotSettingData* data, const QString& newName);
        void synchronousActorName(InfomationGraph3DBasical* info, const QString& newName);

        //左侧关闭工程树，删除相应的图像
        // 注意items是左侧工程树的节点
        void slot_closeTree(const QList<QStandardItem*>& items);

    private:
        ThreeDSurfacePlotSettingDialog* m_mainSettingDialog;
        ThreeDSurfacePlotSidebarSettings* m_sidebarSettings;

        QVector<FieldGraphGenerator::ArrayInformation> m_allScalarFieldArray;
        QVector<FieldGraphGenerator::ArrayInformation> m_allVectorFieldArray;

        //QList<QPair<InfomationGraph3DBasical*, ThreeDSurfacePlotSettingData*>> m_dataMap;
        vtkSmartPointer<PropPickerCallbackHander> m_doubleClickCallback;

        //TDSPGeometrySettingData* m_geometrySettingData;
        TDSPCubeAxesActorSettingData* m_cubeAxesSettingData;
        //QHash<QListWidgetItem*, ThreeDSurfacePlotSidebarData*> m_sideBarSettingData;
        QList<vtkSmartPointer<vtkPolyData>> m_addedFarFieldData;

        bool m_isAnimationDataPrepared;
        QList<vtkSmartPointer<vtkPolyData>> m_allAnimationdData;

        int m_windowUniversalID{-1};
        int m_windowId;
        vtkSmartPointer<TDSPDefaultInteractorStyle> m_defaultInteractorStyle;
    };
}

#endif
